<template>
    <div class="user-profile">
      <input
      type="file"
      hidden
      ref="file"
      @change='onFileChange'
    >
        <!-- 导航栏 -->
        <van-nav-bar
        class="page-nav-bar"
        title="个人信息"
        center
        left-arrow @click-left="$router.back()" />
        <!-- /导航栏 -->
        <van-cell title="头像" is-link>
          <van-image
            class="avatar"
            fit="cover"
            round
            :src="user.photo"
          />
        </van-cell>
        <van-cell title="昵称" :value="user.name" is-link @click="isUpdateNameShow = true"/>
        <van-cell title="性别" 
        :value="user.gender == 1? '男':'女'" 
        @click="isUpdateGenderShow = true"
        is-link />
        <van-cell title="生日" 
        :value="user.birthday" 
        @click="isUpdateBirthdayShow = true"
        is-link />

        <!-- 编辑昵称弹层 -->
        <van-popup
        v-model="isUpdateNameShow"
        style="height: 100%;"
        position="bottom">
        <!-- 编辑昵称 -->
        <UpdateName
        v-if="isUpdateNameShow"
        @close='isUpdateNameShow = false'
        v-model="user.name"
        ></UpdateName>
        </van-popup>
        <!-- /编辑昵称弹层 -->

        <!-- 编辑性别弹层 -->
        <van-popup
          v-model="isUpdateGenderShow"
          position="bottom"
        >
          <UpdateGender 
          @close="isUpdateGenderShow = false" 
          v-if="isUpdateGenderShow"
           v-model="user.gender"
          />
        </van-popup>
       <!-- 编辑性别弹层 -->

       <!-- 编辑生日弹层 -->
        <van-popup
        v-model="isUpdateBirthdayShow"
        position="bottom">
          <UpdateBirthday
          v-if="isUpdateBirthdayShow"
          @close="isUpdateBirthdayShow = false"
          v-model="user.birthday"></UpdateBirthday>
        </van-popup>
        <!-- 编辑生日弹层 -->

        <!-- 编辑头像弹层 -->
        <van-popup
        v-model="isUpdatePhotoShow"
        style="height: 100%;"
        position="bottom">
        <UpdatePhoto 
        @close="isUpdatePhotoShow = false"
        :img="img"
        v-if="isUpdatePhotoShow"
        @update-photo="user.photo = $event"
        ></UpdatePhoto>
        </van-popup>
      <!-- 编辑头像弹层 -->
    </div>
</template>

<script>
import {getUserdata} from '@/api/user'
import UpdateName from './components/update-name.vue'
import UpdateGender from './components/update-gende.vue'
import UpdateBirthday from './components/update-birthday.vue'
import UpdatePhoto from './components/update-photo.vue'

export default {
  name: '',
  props: {},
  components: {
    UpdateName,
    UpdateGender,
    UpdateBirthday,
    UpdatePhoto
  },
  data () {
   return {
     user:{}, // 个人信息
     isUpdateNameShow: false, // 编辑昵称
     isUpdateGenderShow:false,  // 编辑性别
     isUpdateBirthdayShow:false,  // 编辑生日
     isUpdatePhotoShow: false,  // 编辑头像
     img:'',
   }
  },
  computed: {},
  watch: {},
  methods: {
    async loadUserdata() {
      try{
        const {data} = await  getUserdata();
        this.user = data.data
        console.log(this.user);
      }catch(err) {
        this.$toast('获取数据失败')
      }
    },
    onFileChange () {
      // 获取文件对象
      const file = this.$refs.file.files[0]
      // 基于文章对象获取 blob 数据
      this.img = window.URL.createObjectURL(file)
      console.log(data)

      // 展示预览图片弹出层
      this.isUpdatePhotoShow = true

      // file-input 如果选了同一个文件不会触发 change 事件
      // 解决办法就是每次使用完毕，把它的 value 清空
      this.$refs.file.value = ''
    }
  },
  created () {
    this.loadUserdata();
  },
  mounted () {
  },
}
</script> 

<style scoped lang="less">
.user-profile {
  .avatar {
    width: 60px;
    height: 60px;
  }
  .van-popup {
    background: #f5f7f9;
  }
  .photo-cell {
    .van-cell__value {
      display: flex;
      flex-direction: row-reverse;
    }
  }
}
</style>